package com.ruoyi.common.pay;


import com.github.binarywang.wxpay.bean.transfer.TransferBatchesRequest;
import com.github.binarywang.wxpay.bean.transfer.TransferBatchesResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.TransferService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.ruoyi.common.config.BaseConfig;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class WxPay {
    /**
     * String partnerTradeNo,Integer amount, String openid
     * @return
     */
    public static Map withdraw(Map param) {
        System.err.println("========================"+BaseConfig.WxPayInit.privateKeyPath);
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(BaseConfig.WxPayInit.APPID);
        wxPayConfig.setMchId(BaseConfig.WxPayInit.MCHID);
        wxPayConfig.setMchKey(BaseConfig.WxPayInit.v3key);
        wxPayConfig.setApiV3Key(BaseConfig.WxPayInit.v3key);
        wxPayConfig.setKeyPath(BaseConfig.WxPayInit.keyPath); // 证书路径，如果使用证书
        wxPayConfig.setCertSerialNo(BaseConfig.WxPayInit.serialNumber);
        wxPayConfig.setPrivateKeyPath(BaseConfig.WxPayInit.privateKeyPath);
        wxPayConfig.setPrivateCertPath(BaseConfig.WxPayInit.privateCertPath);

//        wxPayConfig.setSignType("RSA");
        WxPayService wxPayService=new WxPayServiceImpl();
        // 创建微信支付服务
        wxPayService.setConfig(wxPayConfig);

        Map result=new HashMap();
        String checkName = "NO_CHECK";
        //商户订单号
        //随机获取批次号  切记仅用于测试
        TransferService transferService = wxPayService.getTransferService();
        //获取appId
        String appId = wxPayService.getConfig().getAppId();
        //创建批次对象
        TransferBatchesRequest transferBatchesRequest = new TransferBatchesRequest();
        transferBatchesRequest.setAppid(appId);
//        //设置批次名称 可不写
        transferBatchesRequest.setBatchName("创作收益提现");
//        //设置批次备注 可不写
        transferBatchesRequest.setBatchRemark("创作收益用户提现");
        //设置该批次编号
        transferBatchesRequest.setOutBatchNo(param.get("out_business_no").toString());
        //设置该批次总个数
        transferBatchesRequest.setTotalNum(1);
        BigDecimal amount=new BigDecimal(param.get("amount").toString()).multiply(new BigDecimal(100));
        //设置该批次总金额
        transferBatchesRequest.setTotalAmount(amount.intValue());
        //创建收款人请求对象
        ArrayList<TransferBatchesRequest.TransferDetail> transferDetails = new ArrayList<>();
        TransferBatchesRequest.TransferDetail transferDetail = new TransferBatchesRequest.TransferDetail();
        //转账的编号
        String outDetailNo=param.get("out_business_no").toString();
        transferDetail.setOutDetailNo(outDetailNo);
        //转账的金额
        transferDetail.setTransferAmount(amount.intValue());
        //转账的注释
        transferDetail.setTransferRemark("创作收益");
        //以实际微信公众号那边的openid为准
        String openId=param.get("open_id").toString();
        transferDetail.setOpenid(openId);
        if(null==param.get("payee_real_name")){
            result.put("success","false");
            result.put("state",500);
            result.put("message","该用户还未实名认证");
            return result;
        }
        transferDetail.setUserName(param.get("payee_real_name").toString());
        //把收款人对象放到批次里面
        transferDetails.add(transferDetail);
        transferBatchesRequest.setTransferDetailList(transferDetails);
        TransferBatchesResult transferBatchesResult = null;
        try {
            transferBatchesResult = transferService.transferBatches(transferBatchesRequest);
            if(null!=transferBatchesResult){
                result.put("success","true");
                result.put("state",200);
            }else{
                result.put("success","false");
                result.put("state",500);
                result.put("message","微信转账接口失败");
            }
        } catch (WxPayException e) {
            e.printStackTrace();
            result.put("success","false");
            result.put("state",e.getErrCode());
            result.put("message",e.getMessage());
        }
        return result;
    }
}